package com.example.lotteryticketdev.tool;

import com.alibaba.fastjson.JSONObject;
import com.example.lotteryticketdev.entity.LuckyBall;
import com.example.lotteryticketdev.entity.SpiderResult;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

public class JsonReader {

    public static List<String> getJsonString(String path){
        Reader reader = null ;
        BufferedReader br = null ;
        StringBuilder sb = new StringBuilder();
        List<String> list = new ArrayList<String>();
        try {
            reader  = new FileReader(path);
            br = new BufferedReader(reader);

            String line ;
            int count = 0 ;
            boolean flag = false;
            while ( (line = br.readLine()) != null ){
                if(line.contains("\"name\": \"双色球\"")){
                    count++;
                    flag = true ;
                    sb.append("{");
                }
                if (line.contains("]")){
                    flag = false ;
                    sb.append(line +"}");
                    list.add(sb.toString());
                    sb.delete(0, sb.length());
                }
                if (flag == true){
                    sb.append(line);
                }

            }
            System.out.println("count = " + count);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
           MyClose.closeIO(br,reader);
        }
        return list;
    }

    public static List<LuckyBall> getObject(List<String> jsonStringList){
        List<LuckyBall> list = new ArrayList<LuckyBall>();
        AtomicReference<Integer> count = new AtomicReference<>(0);
        jsonStringList.stream().forEach(s ->{
            LuckyBall luckyBall = JSONObject.parseObject(s, LuckyBall.class);
            count.getAndSet(count.get() + 1);
//            System.out.println("--->"+count);
//            System.out.println(luckyBall.getMsg());
            list.add(luckyBall);
        });
        return list;
    }



    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        List<String> jsonString = getJsonString("D:\\dev\\ideaProject\\lottery-ticket-dev\\src\\main\\resources\\static\\json\\BALL.json");
        List<LuckyBall> list = getObject(jsonString);
        long end = System.currentTimeMillis();
        System.out.println("===>耗费"+ (end - start) * 1.0 /1000 + "秒");

    }
    

}
